package com.oig.sys.base.controller;


import com.oig.common.module.SysRequest;
import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.base.dto.CountryDto;
import com.oig.sys.base.service.ITbCountryService;
import com.oig.sys.base.vo.CountryVo;
import com.oig.sys.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "国别地区管理")
@RestController
@RequestMapping("/country")
@Slf4j
public class TbCountryController {

    private final ITbCountryService countryService;

    @Autowired
    public TbCountryController(ITbCountryService countryService){
        this.countryService = countryService ;
    }

    @PreAuthorize("@pms.hasPermission('/country/addCountry')")
    @SysLog(value="添加国别地区",apiUrl="/country/addCountry")
    @Operation(summary = "添加国别地区")
    @PostMapping(value = "/addCountry")
    public CountryVo addCountry(@RequestBody @Validated(GroupAdd.class)  CountryDto countryDto){
        log.info("add country {}",countryDto);
        return this.countryService.addTbCountry(countryDto);
    }

    @PreAuthorize("@pms.hasPermission('/country/updateCountry')")
    @SysLog(value="修改国别地区",apiUrl="/country/updateCountry")
    @Operation(summary = "修改国别地区")
    @PostMapping(value = "/updateCountry")
    public CountryVo updateCountry(@RequestBody @Validated(GroupEdit.class) CountryDto countryDto){
        log.info("update country {}",countryDto);
        return this.countryService.updateTbCountry(countryDto);
    }

    @PreAuthorize("@pms.hasPermission('/country/cancelCountry')")
    @SysLog(value="逻辑删除国别地区",apiUrl="/country/cancelCountry")
    @Operation(summary = "逻辑删除国别地区")
    @PostMapping(value = "/cancelCountry/{code}")
    public SysResponse<String> cancelCountry(@PathVariable("code") String code){
        log.info("cancel Country code={}", code) ;
        this.countryService.cancelTbCountry(code);
        return SysResponse.ok(code,"逻辑删除国别地区成功");
    }


    @Operation(summary = "查询国家地区,根据上级编码查询")
    @PostMapping(value = "/listCountry/{parentCode}")
    public List<CountryVo> listCountry(@PathVariable("parentCode") String parentCode){
        log.info("list Country, {}", parentCode);
        return this.countryService.listCountry(parentCode);
    }


    @Operation(summary = "查询国家地区,多条件")
    @PostMapping(value = "/findCountry")
    public List<CountryVo> findCountry(@RequestBody SysRequest<CountryDto> countryRequest){
        log.info("find Country:{}", countryRequest);
        return this.countryService.findCountry(countryRequest);
    }


    @Hidden
    @PostMapping(value = "/getCountryByCode/{code}")
    public CountryVo getCountryByCode(@PathVariable(value = "code") String code){
        log.info("code:{}", code);
        return this.countryService.getCountryByCode(code);
    }

    @Hidden
    @PostMapping(value = "/getCountryByApplyCode/{applyCode}")
    public CountryVo getCountryByApplyCode(@PathVariable(value = "applyCode") String applyCode){
        log.info("applyCode:{}", applyCode);
        return this.countryService.getCountryByApplyCode(applyCode);
    }

    @Hidden
    @PostMapping(value = "/findCountriesByApplyCodes")
    public List<CountryVo> findCountriesByApplyCodes(@RequestBody List<String> applyCodeList){
        log.info("applyCode list:{}", applyCodeList);
        return this.countryService.findCountriesByApplyCodes(applyCodeList);
    }

    @Hidden
    @PostMapping(value = "/findCountriesByNames")
    public List<CountryVo> findCountriesByNames(@RequestBody List<String> nameList){
        log.info("name list:{}", nameList);
        return this.countryService.findCountriesByNames(nameList);
    }

    @Hidden
    @PostMapping(value = "/getCountryByName/{name}")
    public CountryVo getCountryByName(@PathVariable(value = "name") String name){
        log.info("name:{}", name);
        return this.countryService.getCountryByName(name);
    }

}
